import { ColumnDef } from '@tanstack/react-table'
import type { PluginLocal, PluginLocalTool } from '@shared/types/entity'
import { Button } from '@renderer/components/ui/button'
import {
  DropdownMenu,
  DropdownMenuContent,
  DropdownMenuItem,
  DropdownMenuSeparator,
  DropdownMenuTrigger
} from '@renderer/components/ui/dropdown-menu'
import usePluginLocalToolStore from '@renderer/store/plugin-local-tool'
import { useShallow } from 'zustand/react/shallow'
import { DrawerTrigger } from '@renderer/components/ui/drawer'
import { MoreHorizontal } from 'lucide-react'
import { useMemo } from 'react'
import usePluginLocalStore from '@renderer/store/plugin-local'

const deleteByMark = async (mark: string) => {
  await window.api.pluginLocalTool.deleteByMark(mark)
}

export const useItemColumns = (sup: PluginLocal): ColumnDef<PluginLocalTool>[] => {
  const { setDrater } = usePluginLocalStore(
    useShallow((state) => ({
      setDrater: state.setDrater
    }))
  )

  const { initToolData, toolEditForm, toolDebugForm } = usePluginLocalToolStore(
    useShallow((state) => ({
      initToolData: state.initData,
      toolEditForm: state.editForm,
      toolDebugForm: state.debugForm
    }))
  )
  return useMemo((): ColumnDef<PluginLocalTool>[] => {
    return [
      {
        accessorKey: 'mark',
        header: '工具标识'
      },
      {
        accessorKey: 'name',
        header: '工具名称'
      },
      {
        id: 'actions',
        header: '操作',
        size: 100,
        cell: ({ row }) => {
          const payment = row.original
          return (
            <DropdownMenu>
              <DropdownMenuTrigger asChild>
                <Button variant="ghost" className="h-8 w-8 p-0">
                  <span className="sr-only">Open menu</span>
                  <MoreHorizontal className="h-4 w-4" />
                </Button>
              </DropdownMenuTrigger>
              <DropdownMenuContent align="end">
                <DrawerTrigger asChild>
                  <DropdownMenuItem
                    onClick={() => {
                      toolDebugForm(payment, sup)
                      setDrater('tool', true)
                    }}
                  >
                    debug
                  </DropdownMenuItem>
                </DrawerTrigger>
                <DropdownMenuSeparator />
                <DrawerTrigger asChild>
                  <DropdownMenuItem
                    onClick={() => {
                      toolEditForm(payment, sup)
                      setDrater('tool', true)
                    }}
                  >
                    编辑
                  </DropdownMenuItem>
                </DrawerTrigger>
                <DropdownMenuSeparator />
                <DropdownMenuItem
                  onClick={() => {
                    deleteByMark(payment.mark)
                    initToolData()
                  }}
                >
                  删除
                </DropdownMenuItem>
              </DropdownMenuContent>
            </DropdownMenu>
          )
        }
      }
    ]
  }, [sup, initToolData, toolEditForm])
}
